<script setup>
import { ref } from 'vue'
import {selectCompanyListService,
    updateCompanyListService,
    insertCompanyListService
} from '@/api/CompanyManager.js'

const total = ref(0)
const params = ref({
    companyName:'',
  pageNum:1,
  pageSize:5
})
const handleClick = () => {
  console.log('click')
}

const xsCompanyList = ref([])
const xsCompanyVoRef = ref()
const xsCompanyVo = ref({
      id:'',
      companyName:'',
      companyDesc:'',
      companyLocation:'',
      companyPhone:'',
      testHc:'',
      onlineHc:'',
})
const rules = {
    companyName:[{required:true,message:'请输入公司名称',trigger:'blur'}],
    companyPhone:[{required:true,message:'请输入公司手机号',trigger:'blur'}],
    testHc:[{required:true,message:'请输入实习岗位',trigger:'blur'}],
    onlineHc:[{required:true,message:'请输入正式岗位',trigger:'blur'}],
}

const selectCompanyList = async() => {
    const res = await selectCompanyListService(params.value)
    xsCompanyList.value = res.data.data
    total.value = res.data.total
}
selectCompanyList()
const showDrawer = ref(false)
const editRow = (row) => {
    showDrawer.value=true
    xsCompanyVo.value =true
}
const delRow = (row) => {

}
const editInsertBtn = () => {
    showDrawer.value=true
    xsCompanyVo.value={}
}
const confirmClick = async ()=>{
  await xsCompanyVoRef.value.validate()
  if(xsCompanyVo.value.id){
    await updateCompanyListService(xsCompanyVo.value)
  }else{
    await insertCompanyListService(xsCompanyVo.value)
  }
  
  ElMessage.success('成功！')
  xsCompanyVo.value={}
  showDrawer.value =  false
  selectCompanyList()
}
const search = () => {
    selectCompanyList()
}
</script>

<template>
  <div>
    <div class="cus-header">
        <el-card>
            <div style="display: flex;justify-content: space-between;">
                <div class="search">
                    <div>
                        公司：
                        <el-input v-model="params.companyName" style="width: 240px" placeholder="请输入课程名称" />
                    </div>
                    <div style="margin-left: 20px;">
                        <el-button @click="search" style="width: 80px;" type="primary">搜索</el-button>
                    </div>
                </div>
                <div class="insert">
                    <el-button style="width: 80px;" type="primary" @click="editInsertBtn">新增</el-button>
                </div>
            </div>
            
            
        </el-card>
    </div>
    <div class="cus-body">
        <el-card style="min-height: 650px;">
            <el-table :data="xsCompanyList" style="width: 100%">
                <el-table-column fixed prop="id" label="企业编号" width="150" />
                <el-table-column prop="companyName" label="企业名称" width="120" />
                <el-table-column prop="companyDesc" label="企业描述" width="120" />
                <el-table-column prop="companyLocation" label="企业地址" width="120" />
                <el-table-column prop="companyPhone" label="企业手机号" width="120" />
                <el-table-column prop="testHc" label="实习HC" width="120" />
                <el-table-column prop="onlineHc" label="正式HC" width="620" />
                <el-table-column fixed="right" label="操作" min-width="120">
                    <template #default="{row}">
                        <el-button link type="primary" size="small" @click="editRow(row)">
                            编辑
                        </el-button>
                        <el-button link type="primary" size="small" @click="delRow(row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
    </div>
  </div>

  <!-- 新增或编辑 -->
  <el-drawer v-model="showDrawer" >
        <template #header>
            <h4>操作</h4>
        </template>
        <template #default>
            <div>
                <el-form
                :model="xsCompanyVo" 
                ref="xsCompanyVoRef"
                :rules="rules"
                label-width="auto">
                    <el-form-item label="公司名称" :label-position="left" prop="companyName">
                        <el-input v-model="xsCompanyVo.companyName" />
                    </el-form-item>
                    <el-form-item label="公司描述" :label-position="left" prop="companyDesc">
                        <el-input v-model="xsCompanyVo.companyDesc" />
                    </el-form-item>
                    <el-form-item label="公司地址" :label-position="left" prop="companyLocation">
                        <el-input v-model="xsCompanyVo.companyLocation" />
                    </el-form-item>
                    <el-form-item label="手机号" :label-position="left" prop="companyPhone">
                        <el-input v-model="xsCompanyVo.companyPhone" />
                    </el-form-item>
                    <el-form-item label="实习岗位" :label-position="left" prop="testHc">
                        <el-input v-model="xsCompanyVo.testHc" />
                    </el-form-item>
                    <el-form-item label="正式岗位" :label-position="left" prop="onlineHc">
                        <el-input v-model="xsCompanyVo.onlineHc" />
                    </el-form-item>
                </el-form>
            </div>
        </template>
        <template #footer>
        <div style="flex: auto">
            <el-button @click="showDrawer = false">取消</el-button>
            <el-button type="primary" @click="confirmClick">提交</el-button>
        </div>
        </template> 
    </el-drawer>
</template>


<style lang="scss" scoped>
    .flex-row{
        display: flex;
        flex-direction: row;
    }
    .cus-header{
        .search{
            display: flex;
            flex-direction: row;
        }
    }
    .cus-body{
        margin-top: 15px;
    }
</style>